package com.candoit.webapps.pricing.controllers;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import com.candoit.webapps.pricing.model.Perfil;
import com.candoit.webapps.pricing.model.User;
import com.candoit.webapps.pricing.util.QueryUtil;

@ManagedBean(name = "admUserCtrl")
@SessionScoped
public class AdminUserController extends AbstractController {
	
	private User userSelected;
	
	private String confirmPassword;
	
	private List<Perfil> perfiles = null;
	
	public User getUserSelected() {
		if (userSelected == null){
			userSelected = new User();
		}
		return userSelected;
	}

	public void setUserSelected(User userSelected) {
		this.userSelected = userSelected;
	}

	public void saveUser(){
		if (!validate()){
			return;
		}
		this.save(userSelected);
		userSelected = null;
	}
	
	public void removeUser(){
		this.remove(userSelected);
		userSelected = null;
	}
	
	public List<User> getUsers() {
		List<User> users = QueryUtil.getInstance().getUsers();
		return users;
	}
	
	public void clearData(){
		this.saveSuccess = false;
		this.deleteSuccess = false;
		this.error = false;
		this.message = null;
		this.userSelected = null;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}
	
	private boolean validate(){
		boolean validate = true;
		if (userSelected.getUserName() == null || userSelected.getUserName().equals("")){
			FacesContext.getCurrentInstance().addMessage("saveuserform:username", new FacesMessage(USER_NAME_EMPTY));
			this.message = USER_NAME_EMPTY;
			validate = false;
		}else{
			if (userSelected.getId() == null){
				User user = QueryUtil.getInstance().getUserByUserName(userSelected.getUserName());
				if (user != null){	
					FacesContext.getCurrentInstance().addMessage("saveuserform:username", new FacesMessage(USER_NAME_DUPLICATE));
					validate = false; 
				}
			}	
		}
		if (!userSelected.getKeyWord().equals(confirmPassword)){
			FacesContext.getCurrentInstance().addMessage("saveuserform:password", new FacesMessage(PASSWORD_DISTINTIC));
			validate = false;
		}
		if (userSelected.getNombre() == null || userSelected.getNombre().equals("")){
			FacesContext.getCurrentInstance().addMessage("saveuserform:nombre", new FacesMessage(NAME_USER_EMPTY));
			validate = false;
		}
		if (userSelected.getApellido() == null || userSelected.getApellido().equals("")){
			FacesContext.getCurrentInstance().addMessage("saveuserform:apellido", new FacesMessage(SURNAME_USER_EMPTY));
			validate = false;
		}
		
		return validate;
	}

	public List<Perfil> getPerfiles() {
		return perfiles;
	}

	public void setPerfiles(List<Perfil> perfiles) {
		this.perfiles = perfiles;
	}
	
	public void buscarPerfiles(){
		perfiles = QueryUtil.getInstance().getPerfiles();
	}
	
	
	
}
